//
//  MenuFilterManagerDemoVC.m
//  YXKitOC
//
//  Created by 张鑫 on 2020/7/1.
//  Copyright © 2020 张鑫. All rights reserved.
//

#import "MenuFilterManagerDemoVC.h"
#import "YXMenuFilterManager.h"
#import "YXKitOC.h"
#import "YXTableView.h"
#import "CustomTableViewCell1.h"
#import "FilterView1.h"
#import "YXCollectionView.h"

@interface MenuFilterManagerDemoVC () <YXMenuFilterManagerDelegate, YXMenuFilterManagerDataSource>

@property (nonatomic, strong) FilterView1 *filterView1; /**< <#描述#> */
@property (nonatomic, strong) FilterView1 *filterView2; /**< 描述 */
@property (nonatomic, strong) FilterView1 *filterView3; /**< <#描述#> */
@property (nonatomic, strong) YXMenuFilterManager *menuFilterManager; /**< <#描述#> */
@property (nonatomic, strong) NSMutableArray *listArray; /**< <#描述#> */
@property (nonatomic, strong) YXTableView *tableView; /**< <#描述#> */

@end

@implementation MenuFilterManagerDemoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.filterView1 = [[FilterView1 alloc] initWithFrame:CGRectMake(0, 0, YX_ScreenW(), 100)];
    self.filterView2 = [[FilterView1 alloc] initWithFrame:CGRectMake(0, 0, YX_ScreenW(), 110)];
    self.filterView3 = [[FilterView1 alloc] initWithFrame:CGRectMake(0, 0, YX_ScreenW(), 120)];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.view.backgroundColor = YX_WhiteColor();
    
    /** 筛选项 */
    [self.menuFilterManager reloadData];
    
    NSString *text = @"一、tableview单一子视图自适应，这是最基础最简单的；1、【重点】在tableView创建的时候设置rowHeigh属性，注意千万不要实现行高的代理方法，否则无效！_tableView.rowHeight = UITableViewAutomaticDimension;        100;2、添加到单元格上的子视图必须设置与单元格contentView约束关联，相对布局。这里推荐使用Masonry很好使！如果出现问题，请回头检查一二步！ps:提到tableView，自然就有cell的处理，cell上子视图添加的时候，有两种方式作者：zhanglizzy链接：https://www.jianshu.com/p/e796049bc182来源：简书著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。";
    [self.listArray addObject:@""];
    [self.listArray addObject:@""];
    [self.listArray addObject:@""];
    
    /** 类别 */
    YXTableView *tableView = [[YXTableView alloc] initWithFrame:CGRectMake(0, YX_SafeArea_Top() + self.menuFilterManager.filterBarHeight, self.view.width, self.view.height - self.menuFilterManager.filterBarHeight - YX_SafeArea_Top())];
    tableView.isDynamicCellHeight = YES;
    tableView.tag = 1000;
    
    [tableView registCells:@[[CustomTableViewCell1 class]] returnClass:^Class(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath, id  _Nonnull cellData) {
        return [CustomTableViewCell1 class];
    } loadCell:^(UITableView * _Nonnull tableView, CustomTableViewCell1 *  _Nonnull loadCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull cellData) {
        
        ((UILabel *)[loadCell viewWithTag:100]).text = [text substringToIndex:arc4random() % text.length];
        ((UILabel *)[loadCell viewWithTag:200]).text = [text substringToIndex:arc4random() % text.length];
        
    } returnHeight:^CGFloat(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath, id  _Nonnull cellData) {
        return UITableViewAutomaticDimension;
    } didSelected:^(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath, id  _Nonnull cellData) {
        
    } loadData:^(NSInteger pageIndex, NSInteger pageSize, YXStatusBlock  _Nonnull statusBlock) {
        statusBlock(nil, self.listArray);
    }];
    
    [tableView reloadData];
    
    [self.view addSubview:tableView];
}

#pragma mark - YXMenuFilterManagerDataSource

/// 设置筛选项标题
/// @param menuFilterManager 筛选管理器
- (NSArray *)titlesOfFilterInYXMenuFilterManager:(YXMenuFilterManager *)menuFilterManager {
    return @[@"品牌", @"里程", @"价格"];
}

/// 设置筛选项内容
/// @param menuFilterManager 筛选管理器
/// @param index 筛选项索引
- (UIView<YXMenuFilterCustomViewProtocol> *)viewOfFilterInYXMenuFilterManager:(YXMenuFilterManager *)menuFilterManager index:(NSInteger)index {
    FilterView1 *view = [@[self.filterView1, self.filterView2, self.filterView3] objectAtIndex:index];
    view.backgroundColor = YX_RandomColor();
    return view;
}

- (void)menuFilterManager:(YXMenuFilterManager *)menuFilterManager filtersShowViewHeightDidUpdate:(CGFloat)filtersShowViewHeight {
    YXTableView *tableView = [self.view viewWithTag:1000];
    tableView.minY = YX_SafeArea_Top() + menuFilterManager.filterBarHeight;
    tableView.height = self.view.height - tableView.minY;
}

- (void)menuFilterManager:(YXMenuFilterManager *)menuFilterManager didSelectedFilterObjects:(NSArray<YXFilterObject *> *)objects filterPage:(UIView *)filterPage filterPageIndex:(NSInteger)filterPageIndex {
    [self.listArray removeAllObjects];
    [self.tableView reloadData];
}

#pragma mark - Getter

- (YXMenuFilterManager *)menuFilterManager {
    if (!_menuFilterManager) {
        
        YXMenuFilterConfig *config = [[YXMenuFilterConfig alloc] init];
        config.selectedTextColor = YX_GreenColor();
        
        _menuFilterManager = [[YXMenuFilterManager alloc] initWithFrame:CGRectMake(0, YX_SafeArea_Top(), self.view.width, self.view.height - YX_SafeArea_Top()) showInView:self.view];
        _menuFilterManager.config = config;
        _menuFilterManager.delegate = self;
        _menuFilterManager.dataSource = self;
    }
    return _menuFilterManager;
}

@end
